//
//  GWCollectionsCell.m
//  GiftWanted
//
//  Created by lanou on 15/11/10.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "GWCollectionsCell.h"
#import "GWCollectionsModel.h"
#import "UIImageView+WebCache.h"
#import "GWTopicsViewController.h"
#import "GWRaidersDetailViewController.h"

@interface GWCollectionsCell ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>


@end

@implementation GWCollectionsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumInteritemSpacing = 10;
        layout.minimumLineSpacing = 10;
        layout.itemSize = CGSizeMake(150, 80);
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 100) collectionViewLayout:layout];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"ztCell"];
        [self.contentView addSubview:_collectionView];
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 40, 20)];
        _titleLabel.text = @"专题";
        _titleLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:_titleLabel];
        _button = [UIButton buttonWithType:(UIButtonTypeSystem)];
        [_button setTitle:@"查看全部>" forState:(UIControlStateNormal)];
        [_button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_button addTarget:self action:@selector(handleCheakButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
        _button.titleLabel.font = [UIFont fontWithName:@"TimesNewRomanPS-ItalicMT" size:12];
        _button.frame = CGRectMake(SCREEN_WIDTH-80, 5, 60, 20);
        [self.contentView addSubview:_button];

    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.collectionsArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ztCell" forIndexPath:indexPath];
    GWCollectionsModel *model = [[GWCollectionsModel alloc] init];
    model = _collectionsArr[indexPath.item];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 80)];
    imageView.layer.cornerRadius = 5;
    imageView.layer.masksToBounds = YES;
    [imageView sd_setImageWithURL:[NSURL URLWithString:model.banner_image_url]];
    [cell addSubview:imageView];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GWRaidersDetailViewController *detailVC = [[GWRaidersDetailViewController alloc] init];
    GWCollectionsModel *model = _collectionsArr[indexPath.item];
    detailVC.name = model.title;
    detailVC.id_s = model.id_s;
    [self.naviC pushViewController:detailVC animated:YES];
}

- (void)handleCheakButtonAction {
    GWTopicsViewController *topicVC = [[GWTopicsViewController alloc] init];
    [self.naviC pushViewController:topicVC animated:YES];
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
